﻿#define _CRT_SECURE_NO_WARNINGS
#include "源.c"
void SeqListInit(SeqList* ps)
{
	assert(ps);
	ps->a = NULL;
	ps->capacity = 0;
	ps->size = 0;
}
void SeqCheckList(SeqList* ps)
{
	assert(ps);
	if (ps->capacity == ps->size)
	{
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		SLDateType* tmp = (SLDateType*)realloc(ps->a, newcapacity * sizeof(SLDateType));
		if (tmp == NULL)
		{
			perror("realloc fail");
			return;
		}
		else
		{
			ps->a = tmp;
			ps->capacity = newcapacity;
		}
	}
}
void SeqListDestroy(SeqList* ps)
{
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->capacity = 0;
	ps->size = 0;
}
void SeqListPrint(SeqList* ps)
{
	assert(ps);
	for (int i = 0; i < ps->size; i++)
	{
		printf("%d ", ps->a[i]);
	}
}
void SeqListPushBack(SeqList* ps, SLDateType x)
{
	assert(ps);
	SeqCheckList(ps);
	ps->a[ps->size] = x;
	ps->size++;
}
SLDateType SeqListPopBack(SeqList* ps)
{
	assert(ps);
	if (ps->size == 0)
	{
		printf("没有数据！\n");
		return 0;
	}
	SLDateType as = ps->a[ps->size - 1];
	ps->size--;
	return as;
}
void SeqListPushFront(SeqList* ps, SLDateType x)
{
	assert(ps);
	SeqCheckList(ps);

	for (int i = ps->size; i > 0; i--)
	{
		ps->a[i] = ps->a[i - 1];
	}
	ps->a[0] = x;
	ps->size++;
}
void SeqListPopFront(SeqList* ps)
{
	assert(ps);
	if (ps->size == 0)
	{
		printf("没有数据！\n");
		return;
	}
	for (int i = 0; i < ps->size - 1; i++)
	{
		ps->a[i] = ps->a[i + 1];
	}
	ps->size--;
}
int SeqListFind(SeqList* ps, SLDateType x)
{
	assert(ps);
	for (int i = 0; i < ps->size; i++)
	{
		if (ps->a[i] == x)
		{
			return i;
		}
	}
}
void SeqListInsert(SeqList* ps, int pos, SLDateType x)
{
	assert(ps);
	SeqCheckList(ps);
	if (pos > ps->size)
	{
		ps->a[ps->size] = x;
		ps->size++;
		printf("这个位置超出数据长度，补于末尾！\n");
		return;
	}
	for (int i = ps->size; i > pos - 1; i--)
	{
		ps->a[i] = ps->a[i - 1];
	}
	ps->a[pos - 1] = x;
	ps->size++;
}
void SeqListErase(SeqList* ps, int pos)
{
	assert(ps);
	if (ps->size == 0)
	{
		printf("没有数据！\n");
		return;
	}
	if (ps->size < pos)
	{
		printf("没有这个位置的数据！\n");
		return;
	}
	for (int i = pos; i < ps->size; i++)
	{
		ps->a[i - 1] = ps->a[i];
	}
	ps->size--;
}


int main()
{
	SeqList ts;
	SeqListInit(&ts);
	/*SeqListPushBack(&ts, 1);
	SeqListPushBack(&ts, 2);
	SeqListPushBack(&ts, 3);*/
	/*SLDateType s = SeqListPopBack(&ts);*/
	/*printf("%d \n", s);*/
	SeqListPushFront(&ts, 1);
	SeqListPushFront(&ts, 2);
	SeqListPushFront(&ts, 3);
	SeqListPushFront(&ts, 4);
	/*SeqListPopFront(&ts);
	SeqListPopFront(&ts);
	SeqListPopFront(&ts);
	SeqListPopFront(&ts);
	SeqListPopFront(&ts);*/

	/*SeqListPushFront(&ts, 5);
	SeqListPushFront(&ts, 6);*/
	/*int s = SeqListFind(&ts, 2);
	printf("a[%d]\n", s);*/
	//SeqListErase(&ts, 4);
	///*SeqListErase(&ts, 4);
	//SeqListErase(&ts, 4);
	//SeqListErase(&ts, 4);
	//SeqListErase(&ts, 4);
	//SeqListErase(&ts, 4);
	//SeqListErase(&ts, 4);
	//SeqListErase(&ts, 4);*/
	//SeqListInsert(&ts, 4, 100);

	SeqListPrint(&ts);

	return 0;
}